package com.xs.api.impl;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.xs.api.UserServiceApi;
import com.xs.entity.User;

@RestController
public class UserServiceApiImpl implements UserServiceApi {

	private Log log = LogFactory.getLog(UserServiceApiImpl.class);

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	Environment environment;

	public String getPort() {
		return environment.getProperty("local.server.port");
	}

	

	// @GetMapping("/hello-service")
	public String helloService() {
		log.info("hello-service port is " + getPort());
		String services = "Services: " + discoveryClient.getServices() + getPort();
		log.info(services);
		return services;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xs.api.service.IUserService#getUserById(int)
	 */
	@Override
	public User getUserById(int userId) {
		log.info("hello-service getUserById  is " + getPort());
		User user = new User();
		user.setUserId(userId);
		user.setAge(new Random().nextInt(20));
		user.setUserName("liudehua");
		return user;
	}

	/* (non-Javadoc)
	 * @see com.xs.api.UserServiceApi#post(com.xs.entity.User)
	 */
	@Override
	public User getUserInfo(@RequestBody User user) {
		log.info("hello-service post params   is " + user);
		user.setUserId(user.getUserId());
		user.setAge(new Random().nextInt(20));
		user.setUserName("liudehua");
		return user;
	}

}
